#include<stdio.h>

int main()
{
	int m, n, p, q, r;
	scanf_s("%d", &n);
	for (int j = 0; j < n; j++)
	{
		p = 1;
		q = 1;
        r = 0;
		scanf_s("%d", &m);
        if (m == 2)
            p = 1;
        else if (m % 2 == 0)
            p = 0;
        else
        {
            for (int i = 3; i * i <= m; i += 2)
            {
                if (m % i == 0)
                    p = 0;
            }
        }
        while (m > 0)
        {
            r = r * 10 + m % 10;
            m = m / 10;
        }
        if (r == 2)
            q = 1;
        else if (r % 2 == 0)
            q = 0;
        else
        {
            for (int h = 3; h * h <= r; h += 2)
            {
                if (r % h == 0)
                    q = 0;
            }
        }
        if (p == 1 && q == 1)
            printf("1\n");
        else
        {
            printf("0\n");
        }
	}
    return 0;
}